package acwing._2LanQiao._13No13;
/**
 * @Project : AlgorithmLearning
 * @Package : ACWing._2LanQiao._13No13
 * @File : Q4655.java
 * @Author : WangRuoyu
 * @Date : 2023/3/31 10:21
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class Q4655 {
    static int n;
    static int m;
    static int[] nums;
    static int[] cnts;
    static int[] b;

    static void add(int l, int r, int c) {
        b[l] += c;
        b[r + 1] -= c;
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] words = br.readLine().split(" ");
        n = Integer.parseInt(words[0]);
        words = br.readLine().split(" ");
        cnts = new int[n + 1];
        nums = new int[n];
        b = new int[n + 1];

        for (int i = 0; i < n; ++i) {
            nums[i] = Integer.parseInt(words[i]);
        }

        words = br.readLine().split(" ");
        m = Integer.parseInt(words[0]);
        for (int i = 0; i < m; ++i) {
            words = br.readLine().split(" ");
            int l = Integer.parseInt(words[0])-1;
            int r = Integer.parseInt(words[1])-1;
            add(l, r, 1);
        }
        for (int i = 1; i <= n; ++i) {
            cnts[i] = cnts[i - 1] + b[i - 1];
        }

        long sum = 0;
        for (int i = 0; i < n; ++i) {
            sum += (long) cnts[i + 1] * (long) nums[i];
        }
        Arrays.sort(nums);
        Arrays.sort(cnts);

        long res = 0;
        for (int i = 0; i < n; ++i) {
            res += (long) cnts[i + 1] * (long) nums[i];
        }

        System.out.println(res-sum);
    }
}
